package com.kgc.community.controller;

import com.kgc.community.pojo.*;
import com.kgc.community.service.CostCountService;
import com.kgc.community.service.CostService;
import com.kgc.community.service.CostTypeService;
import com.kgc.community.service.UserService;
import com.kgc.community.service.impl.CostServiceImpl;
import org.bouncycastle.math.raw.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Mr.Man
 * @Date 2020-11-29 13:29
 **/
@Controller
public class CostController {
    @Resource
    CostService costService;
    @Resource
    CostTypeService costTypeService;
    @Resource
    UserService userService;
    @Resource
    CostCountService costCountService;


    /**
     * 收费页面
     * @param model
     * @return
     */
    @RequestMapping("gopro_list.do")
    public String gopro_list(Model model,String type,String descri){
        System.out.println(descri);
        System.out.println(type);
        if (type=="0"){
            type=null;
        }
        if(descri==""){
            descri=null;
        }

        List<CostCount> costCounts = costCountService.selectBy(type, descri);
        List<CostType> costTypes = costTypeService.selectAll();
        model.addAttribute("tp",type);
        model.addAttribute("costTypes",costTypes);
        model.addAttribute("descri",descri);
        model.addAttribute("costCounts",costCounts);
        return "pro_list";
    }

    /**
     * 删除页面
     * @param id
     */
    @RequestMapping("deleteId.do")
    @ResponseBody
    public void delete(Integer id){
        System.out.println(id+"测试001");
        Integer integer = costCountService.deleteById(id);
        System.out.println(integer);
    }

    @RequestMapping("pro_add.do")
    public String pro_add(Model model){
        List<CostType> costTypes = costTypeService.selectAll();
        model.addAttribute("costTypes",costTypes);
        return "pro_add";
    }


    @RequestMapping("gopay.do")
    public String gopay(){
        return "pay";
    }

    @RequestMapping("goindex01.do")
    public String goindex01(){
        return "index01";
    }

    @RequestMapping("notify_url.do")
    public String notify_url(){
        return "notify_url";
    }

    @RequestMapping("return_url.do")
    public String return_url(){
        return "return_url";
    }

    @RequestMapping("addCostCount.do")
    public String addCostCount(CostCount costCount){
        System.out.println(costCount.getUid());
        costCount.setCreatetiem(new Date());
        costCount.setUtype(0);
        Integer integer = costCountService.insertCost(costCount);
        List<Rooms> rooms = costService.selectNoBytypeId();
        for (Rooms room : rooms) {
            Cost cost = new Cost();
            cost.setCost((long) 0);
            cost.setRoomid(Integer.parseInt(room.getId()));
            cost.setTypeId(Integer.parseInt(room.getId()));
            cost.setPaymentTime(new Date());
            cost.setTypecout(costCount.getCid());
            cost.setUserId(0);
            costService.insert(cost);
        };

        return "redirect:gopro_list.do";
    }

    @RequestMapping("gopromx_list.do")
    public String gopromx_list(Model model,Integer id){
        List<Cost> costs = costService.selectBycid(id);
        model.addAttribute("cost",costs);
                return "promx_list";
    }

    @RequestMapping("goaddover.do")
    public String goaddover(HttpSession session){
        String wiDout_trade_no = (String) session.getAttribute("coutappl");
        System.out.println(wiDout_trade_no+"测试");
        Integer integer = costService.updateType(Integer.parseInt(wiDout_trade_no));
        System.out.println(integer+"测试成功");
        return "index";
    }

}
